package com.gxa.medical.controller;

import com.gxa.medical.mapper.TalkMapper;
import com.gxa.medical.pojo.Talk;
import com.gxa.medical.service.TalkService;
import com.gxa.medical.service.impl.TalkServiceImpl;
import com.gxa.medical.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/talkht")
@Api("病例探讨相关api接口")
@CrossOrigin
public class TalkContorller {
    @Autowired
    private TalkService talkService;

    @PostMapping("/all")
    @ApiOperation(value = "分页查询全部探讨的方法")
    public Map selectAllByPage(Integer page,Integer size){
        System.out.println("page:"+page);
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> maps = talkService.selectAllByPage(page, size);
        Integer integer = talkService.selectCon();
        Map map = new HashMap<>();
        map.put("maps",maps);
        map.put("integer",integer);
        jsonResult.setCode("200");
        jsonResult.setData(map);
        return jsonResult.getResultMap();
    }
    @PostMapping("/like")
    @ApiOperation(value = "模糊查询的方法")
    public Map selectByLike(String name,String sigstaTime,String sigoverTime,Integer isud,Integer page,Integer size){
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> maps = talkService.selectByLike(name, sigstaTime, sigoverTime, isud, page, size);
        int size1 = maps.size();
        Map map = new HashMap();
        map.put("maps",maps);
        map.put("size",size1);
        jsonResult.setCode("200");
        jsonResult.setData(map);
        return jsonResult.getResultMap();
    }

    @PostMapping("/delect")
    @ApiOperation(value ="删除数据的方法")
    public Map delectById(Integer id){
        JsonResult jsonResult = new JsonResult();
        Integer integer = talkService.delectById(id);
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getResultMap();
    }
    @PostMapping("/add")
    @ApiOperation(value = "添加数据的方法")
    public Map insert(Talk talk,String hname,String[] iname,String uname){
        JsonResult jsonResult = new JsonResult();
        talkService.insert(talk);
        Integer talk_id = talk.getTalk_id();
        Integer doc_id = talkService.selectByDocName(hname);
        talkService.insertDT(doc_id,talk_id);
        talkService.insertTPS(talk_id,doc_id);
        for (int i=0;i<iname.length;i++){
            Integer invite_doc_id = talkService.selectByDocName(iname[i]);
            talkService.insertInD(invite_doc_id,talk_id);
            talkService.insertTPS(talk_id,invite_doc_id);
        }
        Integer user_id = talkService.selectByUserName(uname);
        talkService.insertUT(user_id,talk_id);
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getResultMap();
    }
    @PostMapping("/doc")
    @ApiOperation(value = "查询所有医生名字的方法")
    public Map selectAllDoc(){
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> maps = talkService.selectAllByim();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        return jsonResult.getResultMap();
    }
    @PostMapping("/user")
    @ApiOperation(value = "查询所有用户名字的方法")
    public Map selectAllUser(){
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> maps = talkService.selectAllByNa();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        return jsonResult.getResultMap();
    }
    @PostMapping("/xq")
    @ApiOperation(value = "详细查询方法")
    public Map selectByI(Integer id){
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> maps = talkService.selectByI(id);
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        return jsonResult.getResultMap();
    }
    @PostMapping("/lxq")
    @ApiOperation(value = "详情查询列表的方法")
    public Map selectById(Integer id){
        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> maps = talkService.selectById(id);
        int size = maps.size();
        Map map = new HashMap();
        map.put("maps",maps);
        map.put("size",size);
        jsonResult.setCode("200");
        jsonResult.setData(map);
        return jsonResult.getResultMap();
    }
    @PostMapping("/update")
    @ApiOperation(value = "修改数据的方法")
    public Map update(Talk talk,String hname,String[] iname,String uname,Integer id){
        JsonResult jsonResult = new JsonResult();
        talkService.insert(talk);
        Integer talk_id = talk.getTalk_id();
        Integer doc_id = talkService.selectByDocName(hname);
        talkService.insertDT(doc_id,talk_id);
        talkService.insertTPS(talk_id,doc_id);
        for (int i=0;i<iname.length;i++){
            Integer invite_doc_id = talkService.selectByDocName(iname[i]);
            talkService.insertInD(invite_doc_id,talk_id);
            talkService.insertTPS(talk_id,invite_doc_id);
        }
        Integer user_id = talkService.selectByUserName(uname);
        talkService.insertUT(user_id,talk_id);
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        talkService.delectById(id);
        return jsonResult.getResultMap();
    }
}
